<?php
/**
 * 图片处理
 */
class Thumbpic {
	/**
	 * save folder
	 */
	private $savefolder; //注意
	private $savefilename;
	private $ext;
	
	private $img; //图像资源
	private $thumbimg; //缩略图资源
	
	public $fileupload;
	public $extfilter;
	
	public $watermark;
	
	public $date; //存储时间。以保持缩略图和文件名一致
	
	public function __construct($file, $savefolder, $extfilter=array('jpg','png','gif')){
		
		$this->extfilter=$extfilter;		
		//扩展名
		$this->ext =strtolower( end(explode('.',$file['name'])) );
		if(!in_array($this->ext, $this->extfilter)){
			die(' this type file is not allowed ');
		}		
		$this->fileupload = $file;
		if( substr($savefolder,-1) != '/' ) $savefolder.='/';
		$this->setSaveFolder( dirname(__FILE__) .$savefolder);
		
		//由年月构成文件夹，日时分秒构成文件名
		$this->date = array('folder'=>date("Ym"), 'file'=>date("dHis"));
	}
	
	public function setSaveFolder($savefolder){		
		$this->savefolder =  $savefolder;
	}
	
	/**
	 * 主处理
	 *
	 * @param unknown_type $file
	 */
	public function cp(){
		$dir=$this->date['folder'];
		if(!file_exists( $this->savefolder . $dir)) mkdir($this->savefolder . $dir);
		move_uploaded_file( $this->fileupload['tmp_name'] ,$this->savefolder.$dir. '/'.$this->date['file'].'.'.$this->ext);
		$fullfilename=$this->savefolder.$dir. '/'.$this->date['file'].'.'.$this->ext;
		$this->savefilename = $fullfilename;
		switch ($this->ext){
			case 'jpg':
				$img=imagecreatefromjpeg($fullfilename); break;
			case 'gif':
				$img=imagecreatefromgif($fullfilename);
				break;
			case 'png':
				$img=imagecreatefrompng($fullfilename);
				break;
			default:
				$img=imagecreatefromjpeg($fullfilename); break;
			
		}		
		$this->img = $img;
		
		//处理图片水印
		if($this->watermark){
			$waterimg = imagecreatefrompng($this->watermark);
//			imagecopymerge($waterimg,$this->img,10,10,10,10,205,50,30);
			imagecopy($this->img,$waterimg,5,5,0,0,205,50);
			switch ($this->ext){
				case 'gif':
					imagegif($this->img,$fullfilename);break;
				case 'png':
					imagepng($this->img,$fullfilename);break;
				case 'jpg':
					imagejpeg($this->img,$fullfilename);break;
				default:
					imagejpeg($this->img,$fullfilename);break;
			}
		}
	}
	
	/**
	 * 生成缩略图程序
	 *
	 * @param unknown_type $img 图片资源
	 * @param unknown_type $to_w 最后的宽
	 * @param unknown_type $to_h 最后的高
	 */
	public function thumb($to_w, $to_h){
		//计算宽高比，生成与之对应比例图片
		$width=$to_w; $height=$to_h;
		// Get new dimensions
		list($width_orig, $height_orig) = getimagesize($this->savefilename);
		
		if ($width && ($width_orig < $height_orig)) {
		    $width = ($height / $height_orig) * $width_orig;
		} else {
		    $height = ($width / $width_orig) * $height_orig;
		}
		$image_p = imagecreatetruecolor($width, $height);		
		imagecopyresampled($image_p, $this->img, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
		
		// Output
		$dir=$this->date['folder'];
		if(!file_exists( $this->savefolder . $dir)) mkdir($this->savefolder . $dir);
		$thumbfilename=$this->savefolder.$dir. '/'.$this->date['file'].'_thumb.'.$this->ext;
		switch ($this->ext){
			
			case 'gif':
				imagegif($image_p, $thumbfilename, 100);
				break;
			case 'jpg':
			case 'png':
				default:
				imagejpeg($image_p, $thumbfilename, 100);
				break;			
		}		
		imagedestroy($image_p);
	}
	
	public function __destruct(){
		imagedestroy($this->img);
		
	}
	
}

header('Content-Type: text/html; charset=utf-8'); 

$img='';
var_dump($_POST);
if(!empty($_FILES)){
	$img=$_FILES['f'];
	var_dump($img);
	//move_uploaded_file($img['tmp_name'],'./'.$img['name']);
	
	$thumb = new Thumbpic($img,'/b');
	$thumb->watermark= 'watermark.png';
	$thumb->cp(); $thumb->thumb(100,100);
}

//var_dump( dirname(__FILE__) ); die( DIRECTORY_SEPARATOR );
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf8">
		<title>传图</title>
	</head>
	<body>
		<form enctype="multipart/form-data" method="POST">
			
			上传
			<input type="text" name="n" /> 
			<input type="file" name="f" id="f" style="border:1px solid #ccc" />
			<input type="submit" value=" 确定" />
		</form>
	</body>
</html>
